package com.pengtu.gsj.service.impl.check.lzgl.basEduca;

import java.util.ArrayList;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.lzgl.BudgetDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.Budget;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.basEduca.EnforFundService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class EnforFundServiceImpl implements EnforFundService {

	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private BudgetDao budgetDao;
	
	@Override
	public Map<String, Object> getEnforFunds(String startTime, String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> list = new ArrayList<Attachment>();
		List<Budget> budgets = new ArrayList<Budget>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		String year = (String)jsonObject.get("itemProjectId");
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		double tableFull = MathsUtils.formatDouble(yearFull + yearPart, 1);
		double percentage = 0; //完善率
		double total = 0; //计算总分
		
		//根据时间、标签查询是否有附件
		list = systemManageService.getAllCheckAttachmentsByParTag(tag);
		if (StringUtils.isNotEmpty(year)) {
			budgets = budgetDao.findBy("year", year, "year", true);
			if (budgets.size() > 0) {
				Budget budget = budgets.get(0);
				//1、如果路政执法工作经费纳入财政预算管理，得分
				if ("1".equals(budget.getIfInto())) {
					total += yearFull;
				}
				//2、计算预算如果符合经费规模的(±200万)，得分
				double totalBudge = budget.getBudgetPro() + budget.getBudgetary() + budget.getBudgetCou() - (budget.getActRoadBudget() + budget.getActComBudget());
				if (totalBudge <= poitMark && totalBudge >= -poitMark) {
					total += yearPart / 2;
				}
				//3、如果核定人员数符合配置（±1个人）
				int totalPeo = budget.getPersonCount() - (budget.getActRoadPeo() + budget.getActComPeo());
				if (totalPeo <= reserveMark && totalPeo >= -reserveMark) {
					total += yearPart / 2;
				}
			}
			map.put("flag", true);
		}
		total = total < 0 ? 0 : MathsUtils.formatDouble(total, 1);
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		
		//只有当系统完善度和检查得分不相等的情况下才更新数据  避免每次搜索都更新数据
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, true);
		map.put("att", list);
		map.put("manageData", budgets);
		map.put("percent", percentage);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		return map;
	}
	
	/**
	 * 将检索条件封装，进行Budget的文件查询
	 */
	@Override
	public List<Budget> getBudget(String startTime, String endTime, String id) {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("year", startTime, MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("year",  endTime, MatchType.LE);
		}
		if (StringUtils.isNotEmpty(id)) {
			queryFilter.addFilter("id", id, MatchType.EQ);
		}
		return budgetDao.find(queryFilter);
	}

	@Override
	public List<Budget> getEnforFundYear(QueryFilter queryFilter) {
		return budgetDao.find(queryFilter, "year", true);
	}
}
